//
//  WebReplaceV.swift
//  AliBaiChuan
//
//  Created by 郑东喜 on 2017/2/21.
//  Copyright © 2017年 郑东喜. All rights reserved.
//  替换网页

import UIKit

var _webArray : NSMutableArray = NSMutableArray()

class WebReplaceV: BaseWebView {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        if _webArray.count > 0 {
            self.weView.loadRequest(URLRequest.init(url: URL.init(string: (_webArray.lastObject as? String)!)!))
        
        } else {

        }

    }
}

extension WebReplaceV {
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        let urlString = request.url?.absoluteString
    
        print("\((#file as NSString).lastPathComponent):(\(#line))\n",urlString as Any)

        //根据单机状态跳转
        if navigationType == .linkClicked || navigationType == .formResubmitted || navigationType.rawValue == 0 {
            
            
            self.url = urlString!
            
            if _webArray.count > 0 && _webArray.lastObject as? String == urlString  {
                _webArray.removeLastObject()
                return true
            }
            
            _webArray.add(urlString as Any)
            
            self.jumpReVi(replaceURL: urlString!)
  
            
            return false
        } else {
            return true
        }
        
        
        return true
    }
}

// MARK:- 跳转替身网页
extension WebReplaceV {
    
    /// 替身网页跳转
    ///
    /// - Parameter url: 替身网页跳转的url
    fileprivate func jumpReVi(replaceURL url: String) -> Void {
        
        let vvv : WebView = WebView()
        vvv.url = self.url
        self.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vvv, animated: true)
    }
}
